<?php
namespace app\admin\controller;

use app\admin\my\MyPage;
use app\admin\my\MyConfig;


class We extends Base {
   
	
    function _initialize()
    {
        $this->we_type = [ '订阅号', '认证订阅号', '服务号','认证服务号' ];
        $this->sex = ['未知','男','女'];
        $this->qrcode_type=['临时二维码','数值型永久二维码','字符型永久二维码'];
    }

    public function we() {
		$my = new MyPage();
	   
	    $my ->title("微信公众号管理")
		    ->suggest("微信公众号列表")

		    ->buttonNew()
		    ->buttonTrash('we')
		    ->buttonRestore('we')
		    ->buttonEnable('we')
		    ->buttonDisable('we')

		    ->setDb('we')
		    ->keyId()
		    ->keyLink('title', '名称','we/index/index?appid={$appid}','new')
		    ->keyText('description', '简介')
		    ->keyMap('we_type', '类型', $this->we_type)
		    ->actionEdit()
		    ->action('getCards?appid={$appid}','拉取卡券')
		    ->actionStatus()
		    ->map('title', '名称', '微信公众号昵称')
		    ->map('wechat_type', '类型', '微信公众号类型', 'select', $this->we_type)
		    ->mapStatus();

    	if ( request()->isAjax() ) {
    		return my_data($my,'We');
	    } else {
		    return $my->fetch();
	    }
    }



    public function weEdit($id=0) {
		if ( request()->isPost() ) {
			$res = model('we/We')->editData();
			if ($res>0) $this->success('更新成功','we');
			$this->error('更新失败');
		} else {
			$data=my_edit('we/We',$id);

			$rob_array = db("rob")
			->field("id,title")
			->where([
				'status' => ['gt', -1],
			])->select();

			$my = new MyConfig();
			return  $my->title("编辑微信公众号")->keyId()
			    ->keyText('title', '名称')
			    ->keySelect('we_type', '类型', '', false, $this->we_type)
			    ->keyText('description', '简介')
			    
			    ->keyText('token', 'Token')
			    ->keyText('encodingaeskey', 'EncodingAESKey')
			    ->keyText('appid', 'AppID')
			    ->keyText('appsecret', 'AppSecret')
			    
			    ->keyText('mchid', 'mchid')
			    ->keyText('pay_key', 'pay_key')
			    ->keyText('apiclient_cert', 'apiclient_cert')
			    ->keyText('apiclient_key', 'apiclient_key')
			    // ->keyChosen('rob','机器人', null, $rob_array)
			    ->keySelect2('rob','机器人', null, $rob_array)
			    ->keyImage('qrcode', '二维码')
			    ->keyMultiImage('tui_pic', '推广背景图')

			    ->group( '基本', 'fa-cog', 'id,title,we_type,desc,token,encodingaeskey,appid,appsecret')
			   
			    ->group( '微信支付', 'fa-wechat', 'mchid,pay_key,apiclient_cert,apiclient_key')
			    ->group( '机器人', 'fa-wechat', 'rob')
			     ->group( '运营资料', 'fa-wechat', 'qrcode,tui_pic')
			    ->buttonSubmit()->buttonBack()
                ->data($data)->fetch();
		}
	}


	 public function fans() {
		$my = new MyPage();
	    $Wes=my_select('We','appid','title');
	    $my ->title("微信粉丝管理")
		    ->suggest("好的生意就是用心经营好每一个顾客")
		    ->buttonNew('fansEdit')
		    
		    ->keyId()
		    ->keyText('nickname', '昵称')
		    ->keyText('remark', '备注')
		    ->keyMap('sex', '性别', ['未知','男','女'])
		    ->keyText('city', '城市')
		    ->keyJoin('appid', '微信','appid','title','we')
		    ->keyMap('subscribe', '关注', ['-','ok'])
		    

		    ->actionEdit()
		    ->actionStatus()
		    ->actionDelete()

		    
		    ->map('nickname', '昵称')
		    ->map('appid', '微信', 'select', $Wes)
		    ->map('wechat_type', '性别', 'select', ['未知','男','女'])
		    
		    ->mapStatus()->mapTime();

    	if ( request()->isAjax() ) {
    		return my_data($my,'WeFans');
	    } else {
		    return $my->fetch();
	    }
    }


    public function fansEdit($id=0) {
		

		if ( request()->isPost() ) { 
			$res = model('we/WeFans')->editData();
			if ($res>0) $this->success('更新成功','we');
			$this->error('更新失败','we');
		} else {
			
			$data=my_edit('we/WeFans',$id);

			$my = new MyConfig();
			return  $my->title("编辑微信公众号")->keyId()
					    ->keyText('nickname', '昵称')->keyText('remark', '备注')
					    ->keySelect('sex', '性别', null, false,  $this->sex)
					    

					    ->group('basic', '基本', 'fa-cog', 'id,nickname,remark')
					    ->group('wechat', '微信', 'fa-wechat', 'sex')
					    ->buttonSubmit()->buttonBack()
                        ->data($data)->fetch();
		}
	}

	// 财产记录
	public function cfo() {
		$my = new MyPage();
	   
	    $my ->title("粉丝财产流水记录")
		    ->suggest("记录粉丝每一次财产流水")
		   
		    ->setDb('WeFansCfo')
		    ->keyText('nickname', '粉丝')
		    ->keyText('appid', '微信')
		    ->keyText('qrcode', '二维码')
		    ->keyJoin('shopid', '分店', 'id', 'title', 'admin_shop')
		    ->keyMap('type', '类型', ['储值','积分'])
		    ->keyMap('pay_type', '支付', ['现金','微信'])
		    ->keyText('um', '数量')
		    ->keyText('balance', '余额')
		    ->keyCreateTime()
		   
		    ->actionEdit()
		    ->actionStatus()
		    ->map('title', '名称', '微信公众号昵称')
		    ->mapStatus();

    	if ( request()->isAjax() ) {
    		return my_data($my,'WeFansCfo');
	    } else {
		    return $my->fetch();
	    }
    }

    // 财产记录
	public function buy() {
		$my = new MyPage();
	   
	    $my ->title("粉丝财产流水记录")
		    ->suggest("记录粉丝每一次财产流水")
		   
		    ->setDb('WeFansBuy')
		    ->keyId()
		    ->keyText('nickname', '粉丝')
		    ->keyJoin('appid', '微信', 'appid', 'title', 'we')
		    
		    ->keyJoin('shopid', '分店', 'id', 'title', 'admin_shop')
		   
		    ->keyText('desk', '标示')
		    ->keyText('price', '金额')
		    ->keyText('score', '积分')
		    ->keyText('reward', '提成')
		    ->keyText('reward_openid', '分销者')
		    ->keyText('name', '操作')
		    ->keyCreateTime()
		   
		    ->actionEdit()
		    ->actionStatus()
		    ->map('title', '名称', '微信公众号昵称')
		    ->mapStatus();

    	if ( request()->isAjax() ) {
    		return my_data($my,'WeFansBuy');
	    } else {
		    return $my->fetch();
	    }
    }

	 public function menu() {
		$my = new MyPage();
	    $Wes=my_select('We','appid','title');
	    $my ->title("微信导航管理")
		    ->suggest("清晰的导航会让顾客第一眼就喜欢上你")
		    ->buttonNew('menuEdit')
		    
		    ->keyId()
		    ->keyJoin('appid', '微信','appid','title','we')
		    ->keyText('title', '菜单')
		    ->keyText('tag_id', '标签')
		    ->keyMap('sex', '性别',['全部','男','女'])
		    ->keyText('language', '语言')
		    ->keyText('country', '祖国')
		    ->keyText('province', '省份')
		    ->keyText('city', '城市')
		    ->keyText('client_platform_type', '终端')
		    ->actionEdit()
		    ->action('menuButton?menu=####','按钮')
		    ->action('sendMenu?id=####','推送')
		    ->actionStatus()
		    
		    
		    ->map('title', '菜单')
		    ->map('appid', '微信', 'select', $Wes)
		    ->mapStatus();

    	if ( request()->isAjax() ) {
    		return my_data($my,'WeMenu');
	    } else {
		    return $my->fetch();
	    }
    }

    public function sendMenu($id) {
        $res=model('we/WeMenu')->sendMenu($id);
        if (!$res){
        	$this->success('同步成功');
        }else{
        	$this->error($res);
        }

	}


	// 编辑&添加
    public function menuEdit($id=0) {
		

		if ( request()->isPost() ) { 
			$res = model('we/WeMenu')->editData();
			if ($res>0) $this->success('更新成功','menu');
			$this->error('更新失败');
		} else {
			$Wes=my_select('We','appid','title');
			$data=my_edit('we/WeMenu',$id);

			$my = new MyConfig();
			return  $my->title("编辑微信公众号")->keyId()
					    ->keyText('title', '菜单')
					    ->keySelect('appid','微信','','',$Wes)
					    ->keySelect('sex', '性别', null, false,$this->sex)
					    ->keySelect('tag_id', 'tag_id', null, false,$this->sex)
					    ->keySelect('language', 'language', null, false,$this->sex)
					    ->keySelect('country', 'country', null, false,$this->sex)
					    ->keySelect('province', 'province', null, false,$this->sex)
					    ->keySelect('city', 'city', null, false,$this->sex)
					    ->keySelect('client_platform_type', 'client_platform_type', null, false,$this->sex)
					    
					    ->buttonSubmit()->buttonBack()
                        ->data($data)->fetch();
		}
	}

	public function menuButton($menu='') {
		$my = new MyPage();
	   
	    $my ->title("按钮管理")
		    ->suggest("每一个按钮都有一个服务好顾客的心")
		    ->buttonNew('menuButtonEdit?menu='.$menu)
		    
		    ->keyId()
		    ->keyJoin('menu', '菜单','id','title','we_menu')
		    ->keyLink('title_show', '按钮','menuButton?menu={$menu}&pid=####')
		    
		    ->keyText('key', '机器人')
		    ->keyText('sort', '排序')
		    

		    ->actionEdit('menuButtonEdit?menu={$menu}&id=####')
		    ->actionStatus()
		    ->actionDelete()

		    
		    ->map('name', '菜单')
		    ->mapStatus();

    	if ( request()->isAjax() ) {
    		$data=my_data($my,'WeMenuButton',['menu'=>$menu]);
    		$data['rows'] = model('common/Tree')->toFormatTree($data['rows']);
    		return $data;
    		
	    } else {
		    return $my->fetch();
	    }
    }


	
    public function menuButtonEdit($id=0,$menu='') {
		
       
		if ( request()->isPost() ) { 
		    if (!input('menu')) $this->error('请选择所属菜单');
			$res = model('we/WeMenuButton')->editData();
			if ($res>0) $this->success('更新成功',url('menuButton',['menu'=>input('menu')]));
			$this->error('更新失败');
		} else {
			$data=my_edit('we/WeMenuButton',$id);
			$menus=my_select('WeMenu');
			$buttons=my_select('WeMenuButton','id','name',['pid'=>0,'menu'=>$menu]);
			
            $answers=my_select('RobAnswer','keyword','title');
			$my = new MyConfig();
			return  $my->title("编辑按钮")->keyId()
					    ->keyText('name', '按钮')
					    ->keySelect('menu', '菜单', null, false,$menus)
					    ->keySelect('pid', '父按钮', null, false,['0'=>'根按钮']+$buttons)
					    ->keyText('key', '关键词','可以是网址')
					    ->keySelect('key2', '快速选择','','',$answers)
					    ->keyText('sort', '排序')
					   
					  
					    
					    ->buttonSubmit()->buttonBack()
                        ->data($data)->fetch();
		}
	}


    // 场景二维码开始
	public function scene() {
		$my = new MyPage();
	   
	    $my ->title("场景管理")
		    ->suggest("每一个场景就是一个推广渠道")
		    ->buttonNew("sceneEdit")
		    ->setDb('WeScene')
		    ->keyId()
		    ->keyText('title', '名称')
		    ->keyText('desc', '简介')
		    ->keyCreateTime()->keyUpdateTime()
		    ->actionEdit()
		    ->actionStatus()
		    ->map('title', '名称', '微信公众号昵称')
		    ->mapStatus();

    	if ( request()->isAjax() ) {
    		return my_data($my,'WeScene');
	    } else {
		    return $my->fetch();
	    }
    }


    public function sceneEdit($id=0) {
		

		if ( request()->isPost() ) { 

			$res = model('we/WeScene')->editData();
			if ($res>0) $this->success('更新成功','scene');
			$this->error('更新失败');
		} else {
			
			$data=my_edit('we/WeScene',$id);

			$my = new MyConfig();
			return  $my->title("编辑场景")->keyId()
					    ->keyText('title', '名称')
					    ->keyText('desc', '简介')
					   
					    ->buttonSubmit()->buttonBack()
                        ->data($data)->fetch();
		}
	}

	// 场景二维码开始
	public function qrcode() {
		$my = new MyPage();
	   
	    $my ->title("二维码管理")
		    ->suggest("每一个场景就是一个推广渠道")
		    ->buttonNew("qrcodeEdit",'增加临时二维码')
		    ->buttonNew("qrcodeEdit?qrcode_type=1",'增加永久二维码')
		    ->setDb('WeSceneQrcode')
		    ->keyId()
		    ->keyText('title', '名称')
		    ->keyText('desc', '描述')
		    ->keyJoin('shop_id', '分店', 'id', 'title', 'admin_shop')
		    ->keyJoin('scene', '场景', 'id', 'title', 'we_scene')
		    ->keyMap('qrcode_type', '类型', $this->qrcode_type)
		    ->keyText('scene_id', 'scene_id')
		    ->keyText('scene_str', 'scene_str')
		    ->keyText('short_url', '二维码')
		    ->keyText('subscribe', '关注')
		    ->keyText('SCAN', '扫码')
		    ->actionEdit()
		    ->actionStatus()
		    ->map('title', '名称', '微信公众号昵称')
		    ->mapStatus();

    	if ( request()->isAjax() ) {
    		return my_data($my,'WeSceneQrcode');
	    } else {
		    return $my->fetch();
	    }
    }


    public function qrcodeEdit($id=0,$qrcode_type=0) {
		if ( request()->isPost() ) { 
			$res = model('We/WeSceneQrcode')->editData();
			if ($res>0) $this->success('更新成功','qrcode');
			$this->error('更新失败');
		} else {

			$shops=my_select('AdminShop');
			$wes=my_select('we','appid','title');
			$scenes=my_select('WeScene');
			$data=my_edit('we/WeSceneQrcode',$id);
			$data['qrcode_type']=$qrcode_type;
			$my = new MyConfig();
			$my->title("编辑推广二维码")->keyId()
					    ->keyText('title', '二维码')
					    ->keySelect('appid','微信','','',$wes)
					    ->keySelect('shopid','分店','','',$shops)
					    ->keyText('desc', '简介')
					    ->keyText('code', '特殊识别')
					    ->keyHidden('qrcode_type', '类型')
					    ->keyText('subscribe_answer', '关注机器人')
					    ->keyText('SCAN_answer', '扫描机器人');
					    
					    
					   
					 
			if ($qrcode_type==1){
				$my->keyText('scene_str', 'scene_str','可以不填，不填就默认为scene_id')->keySelect('scene','场景','','',$scenes);
			}else{
				$my->keyText('expire', '有效期');
			}

			// $my ->keySelect('userid','类型','',$users);
			$my ->buttonSubmit()->buttonBack()
                ->data($data)
            ->group('基础','fa fa-wechat','title,appid,shopid,desc,qrcode_type,code')
            ->group('机器人','fa fa-wechat','subscribe_answer,SCAN_answer');
            return $my->fetch();
		}
	}

	// 场景二维码开始
	public function scan() {
		$my = new MyPage();
	   
	    $my ->title("扫码记录")
		    ->suggest("每一次扫码都会被记录")
		   
		    ->setDb('WeSceneScan')
		    ->keyId()
		    ->keyText('nickname', '粉丝')
		    ->keyJoin('appid', '微信', 'appid', 'title', 'we')
		    ->keyText('qrcode', '二维码')
		    ->keyJoin('shopid', '分店', 'id', 'title', 'admin_shop')
		    ->keyJoin('scene', '场景', 'id', 'title', 'we_scene')
		    ->keyMap('qrcode_type', '类型', $this->qrcode_type)
		    ->keyMap('do', '动作', ['扫码关注','扫码'])
		    ->keyCreateTime()
		   
		    ->actionEdit()
		    ->actionStatus()
		    ->map('title', '名称', '微信公众号昵称')
		    ->mapStatus();

    	if ( request()->isAjax() ) {
    		return my_data($my,'WeSceneScan');
	    } else {
		    return $my->fetch();
	    }
    }


    public function scanEdit($id=0,$qrcode_type=0) {
		if ( request()->isPost() ) { 
			$res = model('We/WeSceneQrcode')->editData();
			if ($res>0) $this->success('更新成功','we');
			$this->error('更新失败','we');
		} else {

			$shops=my_select('AdminShop');
			$scenes=my_select('WeScene');
			$data=my_edit('we/WeSceneQrcode',$id);
			$my = new MyConfig();
			$my->title("编辑推广二维码")->keyId()
					    ->keyText('title', '二维码')
					    ->keyText('desc', '简介')
					    ->keySelect('shop_id','分店','','',$shops);
					   
					 
			if ($qrcode_type==1){
				$my->keyText('scene_str', 'scene_str','可以不填，不填就默认为scene_id')->keySelect('scene','场景','','',$scenes);
			}else{
				$my->keyText('expire', '有效期');
			}

			// $my ->keySelect('userid','类型','',$users);
			$my ->buttonSubmit()->buttonBack()
                ->data($data);
            return $my->fetch();
		}
	}

	// 场景二维码开始
	public function card($appid='') {
		$my = new MyPage();
	    $wes=my_select('we','appid','title');
	    $my ->title("微信卡券")
		    ->suggest("卡券有价");
		if ($appid) $my   ->buttonNew(url('getCards',['appid'=>$appid]));
		   
		$my->setDb('WeCard')
		    ->keyId()
		    ->keyText('title', '券名')
		    ->keyText('sub_title', '副标题')
		    ->keyText('card_id', 'card_id')
		    ->keyJoin('appid', '微信', 'appid', 'title', 'we')
		    
		    ->keyText('quantity', '库存')
		    ->keyText('total_quantity', '总数')
		    ->keyText('can_share', '分享')
		    
		    ->keyCreateTime()
		   
		    ->actionEdit()
		    ->actionStatus()
		    ->map('title', '名称' )
		    ->map('appid', '微信', 'select',$wes)
		    ->mapStatus();

    	if ( request()->isAjax() ) {
    		return my_data($my,'WeCard');
	    } else {
		    return $my->fetch();
	    }
    }


    public function getCards($appid) {
    	
    	
    	$cards=get_cards($appid);
    	
    	foreach ($cards as $key => $card_id) {
    		$card_info=get_card($appid,$card_id);
    		$type=$card_info['card']['card_type'];
            $card=$card_info['card'][strtolower($type)];
    		$data['appid']=$appid;
    		$data['card_id']=$card_id;
    		$data['title']=$card['base_info']['title'];
    		$data['sub_title']=$card['base_info']['sub_title'];
    		$data['quantity']=$card['base_info']['sku']['quantity'];
    		$data['total_quantity']=$card['base_info']['sku']['total_quantity'];
    		$data['can_share']=$card['base_info']['can_share'];
    		$data['get_limit']=$card['base_info']['get_limit'];
    		$data['can_give_friend']=$card['base_info']['can_give_friend'];
    		$data['status']=$card['base_info']['status'];
    		$res=model('we/weCard')->updateCard($data);

    	}

    	$this->success('成功','card');



	}

	
}